package com.mjfeed.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.RefundMethod;
import com.mjfeed.service.RefundMethodService;
import com.mjfeed.util.Format;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;

/**
 * @description 退款方式
 * @author zhaominglei
 * @date 2014年10月21日
 */
@Controller("adminRefundMethodController")
@RequestMapping("/admin/refundMethod")
public class RefundMethodController extends BaseController {
	@Resource(name="refundMethodService")
	private RefundMethodService refundMethodService;
	
	@RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/refundMethod/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<RefundMethod> pagination = refundMethodService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/refundMethod/list";
	}
	
	@RequestMapping(value = "/add",method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		return "admin/refundMethod/add";
	}
	
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	public String save(RefundMethod refundMethod, HttpServletRequest request, HttpServletResponse response, Model model){
		if (refundMethod == null) {
			return ERROR_VIEW;
		}
		refundMethodService.save(refundMethod);
		return "redirect:/admin/refundMethod/list.shtml";
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		if (id == null || id < 1) {
			return ERROR_VIEW;
		}
		request.setAttribute("refundMethod", refundMethodService.getById(RefundMethod.class, id));
		return "admin/refundMethod/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(RefundMethod refundMethod, HttpServletRequest request, HttpServletResponse response, Model model){
		if (refundMethod == null || refundMethod.getId() == null || refundMethod.getId() < 1) {
			return ERROR_VIEW;
		}
		RefundMethod pRefundMethod = refundMethodService.getById(RefundMethod.class, refundMethod.getId());
		pRefundMethod.setName(refundMethod.getName());
		pRefundMethod.setDescription(refundMethod.getDescription());
		pRefundMethod.setOrders(refundMethod.getOrders());
		refundMethodService.update(pRefundMethod);
		return "redirect:/admin/refundMethod/list.shtml";
	}
}
